<template>
  <section class="todoapp">
    <TodoHeader @add="tianjia"></TodoHeader>
    <TodoMain :list="show" @del="shan"></TodoMain>
    <TodoFooter :obj="list" @qingchu="qc" @dibu="db"></TodoFooter>
  </section>
</template>

<script>
import "./styles/index.css";
import "./styles/base.css";
import TodoHeader from "./components/TodoHeader.vue";
import TodoFooter from "./components/TodoFooter.vue";
import TodoMain from "./components/TodoMain.vue";
// 样式引入 + 导入组件并注册使用
export default {
  components: {
    TodoHeader,
    TodoFooter,
    TodoMain,
  },

  data() {
    return {
      type: "all",
      list: JSON.parse(localStorage.getItem("list")) || [
        {
          id: 100,
          name: "吃饭",
          isDone: true,
        },
        {
          id: 201,
          name: "睡觉",
          isDone: false,
        },
        {
          id: 103,
          name: "打豆豆",
          isDone: true,
        },
      ],
    };
  },
  methods: {
    shan(a) {
      this.list = this.list.filter((item) => item.id !== a);
    },
    tianjia(a) {
      console.log(a);
      this.list.push({
        id: +new Date(),
        name: a,
        isDone: false,
      });
    },
    qc() {
      this.list = this.list.filter((item) => item.isDone === false);
    },
    db(a) {
      this.type = a;
    },
  },
  computed: {
    show() {
      if (this.type === "all") {
        return this.list;
      } else if (this.type === "active") {
        return this.list.filter((item) => !item.isDone);
      } else if (this.type === "complete") {
        return this.list.filter((item) => item.isDone);
      }
    },
  },
  watch: {
    list: {
      deep: true,
      handler(newVal) {
        localStorage.setItem("list", JSON.stringify(newVal));
      },
    },
  },
};
</script>
